package com.ruoyi.supermarket.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.supermarket.domain.SupermarketCommodity;
import com.ruoyi.supermarket.mapper.SupermarketCommodityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.supermarket.mapper.SupermarketStockMapper;
import com.ruoyi.supermarket.domain.SupermarketStock;
import com.ruoyi.supermarket.service.ISupermarketStockService;

/**
 * 进货信息Service业务层处理
 * 
 * @author faye
 * @date 2024-05-28
 */
@Service
public class SupermarketStockServiceImpl implements ISupermarketStockService 
{
    @Autowired
    private SupermarketStockMapper supermarketStockMapper;

    @Autowired
    private SupermarketCommodityMapper supermarketCommodityMapper;

    /**
     * 查询进货信息
     * 
     * @param stockId 进货信息主键
     * @return 进货信息
     */
    @Override
    public SupermarketStock selectSupermarketStockByStockId(Long stockId)
    {
        return supermarketStockMapper.selectSupermarketStockByStockId(stockId);
    }

    /**
     * 查询进货信息列表
     * 
     * @param supermarketStock 进货信息
     * @return 进货信息
     */
    @Override
    public List<SupermarketStock> selectSupermarketStockList(SupermarketStock supermarketStock)
    {
        return supermarketStockMapper.selectSupermarketStockList(supermarketStock);
    }

    /**
     * 新增进货信息
     * 
     * @param supermarketStock 进货信息
     * @return 结果
     */
    @Override
    public int insertSupermarketStock(SupermarketStock supermarketStock)
    {
        supermarketStock.setCreateTime(DateUtils.getNowDate());

        String username= SecurityUtils.getUsername();
        if(StringUtils.isBlank(username)){
            throw new RuntimeException("用户不存在");

        }
       supermarketStock.setCreateBy(username);


        SupermarketCommodity sale_com=
       supermarketCommodityMapper.selectSupermarketCommodityByCommodityId(supermarketStock.getStockCommodityId());
        if(sale_com==null){
            throw new RuntimeException("商品不存在");
        }

        else{
            sale_com.setQuantity(sale_com.getQuantity()+supermarketStock.getStockQuantity());
            supermarketCommodityMapper.updateSupermarketCommodity(sale_com);

            Double cost = sale_com.getCost();
            supermarketStock.setStockCost(cost);
        }


        return supermarketStockMapper.insertSupermarketStock(supermarketStock);
    }

    /**
     * 修改进货信息
     * 
     * @param supermarketStock 进货信息
     * @return 结果
     */
    @Override
    public int updateSupermarketStock(SupermarketStock supermarketStock)
    {
        supermarketStock.setUpdateTime(DateUtils.getNowDate());

        String username= SecurityUtils.getUsername();
        if(StringUtils.isBlank(username)){
            throw new RuntimeException("用户不存在");
        }
       supermarketStock.setUpdateBy(username);

        return supermarketStockMapper.updateSupermarketStock(supermarketStock);
    }

    /**
     * 批量删除进货信息
     * 
     * @param stockIds 需要删除的进货信息主键
     * @return 结果
     */
    @Override
    public int deleteSupermarketStockByStockIds(Long[] stockIds)
    {
        return supermarketStockMapper.deleteSupermarketStockByStockIds(stockIds);
    }

    /**
     * 删除进货信息信息
     * 
     * @param stockId 进货信息主键
     * @return 结果
     */
    @Override
    public int deleteSupermarketStockByStockId(Long stockId)
    {
        return supermarketStockMapper.deleteSupermarketStockByStockId(stockId);
    }
}
